// =================================================================================================
//
//	CopyEngine Framework
//	Copyright 2012 Eran. All Rights Reserved.
//
//	This program is free software. You can redistribute and/or modify it
//	in accordance with the terms of the accompanying license agreement.
//
// =================================================================================================

/**
 * Created by eran on 14-12-21.
 */
package gui.utils.blockEditor
{
    import com.bit101.components.ComboBox;
    import com.bit101.components.PushButton;

    import data.BlockVo;
    import data.DataVoManger;

    import flash.display.MovieClip;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;

    import gui.utils.*;
    import gui.utils.blockEditor.ext.ExtEditPanelBasic;
    import gui.utils.blockEditor.ext.ExtMonsterEditPanel;
    import gui.utils.blockEditor.ext.ExtSkillShopEditPanel;

    public class BlockEditorDialog
    {
        private static var _instance:BlockEditorDialog;

        public static function get instance():BlockEditorDialog
        {
            if (_instance == null)
            {
                _instance = new BlockEditorDialog();
            }
            return _instance;
        }

        private var mDialogLayer:Sprite;
        private var mDialogMc:MovieClip;

        private var mBackgroundShape:Shape;

        private var mXBlockIndexTF:TextField;
        private var mYBlockIndexTF:TextField;
        private var mBlockWidthTF:TextField;
        private var mBlockHeightTF:TextField;

        private var mCacnelBtn:PushButton;
        private var mSaveBtn:PushButton;
        private var mDeleteBtn:PushButton;
        private var mBlockTypeSelector:ComboBox;

        private var mCurrentBlockVo:BlockVo;

        private var mTileVoChangeCallBack:Function;

        private var mAllExtPanelVector:Vector.<ExtEditPanelBasic>;

        public function BlockEditorDialog()
        {
        }

        //=================================//
        //=== Initialize & Dispose ========//
        //=================================//

        public function initialize(_dialogLayer:Sprite, _DialogContainerClass:Class):void
        {
            mDialogLayer = _dialogLayer;
            mDialogMc = new _DialogContainerClass() as MovieClip;
            mDialogLayer.visible = false;

            mBackgroundShape = new Shape();
            mBackgroundShape.graphics.beginFill(0x0, 0.7);
            mBackgroundShape.graphics.drawRect(0, 0, mDialogLayer.stage.stageWidth, mDialogLayer.stage.stageHeight);
            mBackgroundShape.graphics.endFill();
            mDialogLayer.addChild(mBackgroundShape);

            mDialogMc.x = mDialogLayer.stage.stageWidth / 2;
            mDialogMc.y = mDialogLayer.stage.stageHeight / 2;
            mDialogLayer.addChild(mDialogMc);

            mCacnelBtn = new PushButton(null, 0, 0, "Cancel", onClickCancelBtn);
            GUIUtils.replaceTargetWithPH(mCacnelBtn, mDialogMc["cancelBtn"]);

            mSaveBtn = new PushButton(null, 0, 0, "Save", onClickSaveBtn);
            GUIUtils.replaceTargetWithPH(mSaveBtn, mDialogMc["saveBtn"]);

            mDeleteBtn = new PushButton(null, 0, 0, "Delete", onClickDeleteBtn);
            GUIUtils.replaceTargetWithPH(mDeleteBtn, mDialogMc["deleteBtn"]);

            mBlockTypeSelector = new ComboBox(null, 0, 0);
            mBlockTypeSelector.addEventListener(Event.SELECT, onBlockTypeSelectorSelectChange);
            for (var key:String in MapEditorConfig.BLOCK_INFO_OBJECT)
            {
                mBlockTypeSelector.addItem(key);
            }
            mBlockTypeSelector.selectedIndex = 0;
            GUIUtils.replaceTargetWithPH(mBlockTypeSelector, mDialogMc["blockTypeSelector"]);

            mXBlockIndexTF = mDialogMc["xBlockIndexTF"];
            mYBlockIndexTF = mDialogMc["yBlockIndexTF"];
            mBlockWidthTF = mDialogMc["blockWidthTF"];
            mBlockHeightTF = mDialogMc["blockHeightTF"];

            mAllExtPanelVector = new Vector.<ExtEditPanelBasic>();
            doPushExtPanelToVector(new ExtMonsterEditPanel(), "extMonsterPanelMc");
            doPushExtPanelToVector(new ExtSkillShopEditPanel(), "extSkillShopPanelMc");
            refreshExtPanelByCurrentSelectType();
        }

        //=================================//
        //====== Public Function ==========//
        //=================================//

        public function show(_tileIndexX:int, _tileIndexY:int, _tileVoChangeCallBack:Function):void
        {
            mTileVoChangeCallBack = _tileVoChangeCallBack;
            mDialogLayer.visible = true;
            mCurrentBlockVo = DataVoManger.instance.findBlockByTile(_tileIndexX, _tileIndexY);
            if (mCurrentBlockVo)
            {
                mXBlockIndexTF.text = "" + mCurrentBlockVo.x;
                mYBlockIndexTF.text = "" + mCurrentBlockVo.y;
                mBlockWidthTF.text = "" + mCurrentBlockVo.width;
                mBlockHeightTF.text = "" + mCurrentBlockVo.height;
            }
            else
            {
                mXBlockIndexTF.text = "" + _tileIndexX;
                mYBlockIndexTF.text = "" + _tileIndexY;
            }
        }


        //=================================//
        //====== Private Function =========//
        //=================================//


        private function hide():void
        {
            mDialogLayer.visible = false;
        }

        private function doPushExtPanelToVector(_extPanel:ExtEditPanelBasic, _panelKey:String):void
        {
            _extPanel.initialize(mDialogMc[_panelKey]);
            mAllExtPanelVector.push(_extPanel);
        }

        /**
         * 根据当前选择的面板类型来刷新Ext扩展面板
         */
        private function refreshExtPanelByCurrentSelectType():void
        {
            for each(var extPanel:ExtEditPanelBasic in mAllExtPanelVector)
            {
                if (extPanel.isShowPanel(mBlockTypeSelector.selectedItem as String))
                {
                    extPanel.show();
                }
                else
                {
                    extPanel.hide();
                }
            }
        }

        //================
        //==Event Handle
        //================

        private function onBlockTypeSelectorSelectChange(e:Event):void
        {
            refreshExtPanelByCurrentSelectType();
        }

        private function onClickCancelBtn(e:MouseEvent):void
        {
            hide();
        }

        private function onClickSaveBtn(e:MouseEvent):void
        {
            if (mCurrentBlockVo == null)
            {
                mCurrentBlockVo = new BlockVo();
                DataVoManger.instance.addBlockVo(mCurrentBlockVo);
            }

            mCurrentBlockVo.type = MapEditorConfig.getBlockTypeByInfoKey(String(mBlockTypeSelector.selectedItem));
            mCurrentBlockVo.x = int(mXBlockIndexTF.text);
            mCurrentBlockVo.y = int(mYBlockIndexTF.text);
            mCurrentBlockVo.width = int(mBlockWidthTF.text);
            mCurrentBlockVo.height = int(mBlockHeightTF.text);

            for each(var extPanel:ExtEditPanelBasic in mAllExtPanelVector)
            {
                extPanel.saveDataBackToVo(mCurrentBlockVo);
            }

            hide();

            mTileVoChangeCallBack();
        }

        private function onClickDeleteBtn(e:MouseEvent):void
        {
            if (mCurrentBlockVo != null)
            {
                ConfirmBox.instance.ask("是否要删除该Block?", onConfirmDeleteBlock);
            }
            else
            {
                MessageBox.instance.showMsg("还未创建砖块,无法删除");
            }
        }

        private function onConfirmDeleteBlock():void
        {
            hide();

            DataVoManger.instance.deleteBlockVo(mCurrentBlockVo);
            mTileVoChangeCallBack();
        }

    }
}
